<template>
  <el-dialog
    v-model="visible"
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      :rules="dataRule"
      label-width="80px"
      @keyup.enter="onSubmit()"
    >
<!--      <el-form-item-->
<!--        label="供应商名"-->
<!--        prop="name"-->
<!--      >-->
<!--        <el-input v-model="dataForm.name" />-->
<!--      </el-form-item>-->

      <el-form-item  label="供应商名" prop="supplierId">
        <el-select v-model="dataForm.supplierId" filterable  placeholder="请选择" >
          <el-option
            v-for="item in supplieroptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item
        label="商品sku"
        prop="sku"
      >
      <el-input v-model="dataForm.sku" />
      </el-form-item>

      <el-form-item
        label="商品类型"
        prop="skuType"
      >
<!--        <el-select v-model="defunct_skuType_select.select_Name"  placeholder="请选择">-->
<!--          <el-option-->
<!--            v-for="item in supplieroptions"-->
<!--            :key="item.id"-->
<!--            :label="item.name"-->
<!--            :value="item.id"-->
<!--          >-->
<!--          </el-option>-->
<!--        </el-select>-->

        <el-select v-model="dataForm.skuType" placeholder="请选择">
          <el-option
            v-for="item in defunct_skuType_select.selectList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>

<!--          <el-option-->
<!--            v-for="item in supplierTypeoptions"-->
<!--            :key="item.value"-->
<!--            :label="item.label"-->
<!--            :value="item.value">-->
<!--          </el-option>-->
        </el-select>


      </el-form-item>
      <el-form-item
        label="代码"
        prop="code"
      >
      <el-input v-model="dataForm.code" />
      </el-form-item>
      <el-form-item
        label="单价"
        prop="price"
      >
      <el-input-number v-model="dataForm.price"
                       controls-position="right"
                       :precision="2"
                       :max="1000000000"
                       :min="0.01"
      />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button
          @click="visible = false"
        >取消</el-button>
        <el-button

          type="primary"
          @click="onSubmit()"
        >确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { Debounce } from '@/utils/debounce'
const emit = defineEmits(['refreshDataList'])
const visible = ref(false)
const contentEnRef = ref(null)

const supplieroptions = ref(null)
const supplierTypeoptions = ref(null)

const defunct_skuType_select = {
  selectList: [{
    value: '1',
    label: '默认-普通实物商品'
  }, {
    value: '2',
    label: '特殊实物商品'
  }, {
    value: '3',
    label: '橙卷-话费直充'
  }, {
    value: '4',
    label: '橙卷-直充商品'
  }, {
    value: '5',
    label: '橙卷-卡卷商品'
  }],
}

const dataRule = {
  name: [
    { required: true, message: '供应商名不能为空', trigger: 'blur' },
  ],
  sku: [
    { required: true, message: '商品SKU不能为空', trigger: 'blur' },
  ]
}

const dataForm = ref({
  supplierId: '',
  sku: '',
  skuType: '',
  code: '',
  price: '',

});
const dataFormRef = ref(null)
const init = (id) => {
  dataForm.value.id = id || 0
  visible.value = true
  nextTick(() => {
    dataFormRef.value?.resetFields()
    if (dataForm.value.id) {
      http({
        url: http.adornUrl('/admin/suppliersku/queryById'),
        method: 'get',
        params: http.adornParams({id: dataForm.value.id})
      })
        .then(({ data }) => {
          dataForm.value = data
          // handleSupplierIdSelectChange(data.supplierId)
        })
    }
  })
  supplierType();
}
defineExpose({ init })

const handleSupplierIdSelectChange = (value) =>{

  http({
    url: http.adornUrl("/admin/supplier/queryById?id="+value),
    method: "get",
  }).then(({ data }) => {
    supplierTypeoptions.value = data.typeList2;
    // page.total = data.total;
  });


}

const supplierType= (params) =>  {

  http({
    url: http.adornUrl("/admin/supplier/initList"),
    method: "get",
    params: http.adornParams(
      Object.assign(
        {
          current: 1,
          size: 99999,
        },
        params
      )
    ),
  }).then(({ data }) => {
    supplieroptions.value = data.records;
    // page.total = data.total;
  });

}

/**
 * 表单提交
 */
const onSubmit = Debounce(() => {
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      http({
        url: http.adornUrl('/admin/suppliersku/addOrUpd'),
        method: 'post',
        data: http.adornData(dataForm.value)
      })
        .then(() => {
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              visible.value = false
              emit('refreshDataList')
              dataForm.value.content = ''
            }
          })
        })
    }
  })
})

</script>
